<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平时成绩管理系统-课程管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="courseId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="courseName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">教工号</label>
                    <div class="layui-input-block">
                        <input type="text" name="teacherId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">授课老师</label>
                    <div class="layui-input-block">
                        <input type="text" name="teacherName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">课程类型</label>
                    <div class="layui-input-block">
                        <select name="courseType">
                            <option value="">全部</option>
                            <option value="0">专业必修</option>
                            <option value="1">专业选修</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search"
                            data-type="reload">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>

            <table id="LAY-course-manage" lay-filter="LAY-course-manage"></table>

            <script type="text/html" id="layuiadmin-app-student-bar">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>

        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>

<script type="text/html" id="course-type">
    {{# if (d.courseType === 0) { }}
    <span class="layui-badge layui-bg-blue">专业必修</span>
    {{# } else { }}
    <span class="layui-badge">专业选修</span>
    {{# }  }}
</script>
<script>

    function serializeArrayToJson(dataArr) {
        let dataMap = new Map();
        for (let item of dataArr) {
            dataMap.set(item.name, item.value);
        }

        if (dataMap.has("enable")) {
            dataMap.set("enable", 1);
        } else {
            dataMap.set("enable", 0);
        }
        let json = {};
        for (let [k, v] of dataMap) {
            json[k] = v;
        }
        return JSON.stringify(json);
    }

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'course', 'table'], function () {
        let $ = layui.$
            , form = layui.form
            , setter = layui.setter
            , table = layui.table;


        //事件
        let active = {
            /*搜索*/
            reload: function () {
                let params = {
                    courseType: $("select[name='courseType']").val(),
                    courseId: $("input[name='courseId']").val(),
                    courseName: $("input[name='courseName']").val(),
                    teacherName: $("input[name='teacherName']").val(),
                    teacherId: $("input[name='teacherId']").val(),
                };
                //执行重载
                table.reload('LAY-course-manage', {
                    page: {
                        curr: 1
                    }
                    , where: params
                });
            }
            /*
            添加
            * */
            , add: function () {
                layer.open({
                    type: 2
                    , title: '新增课程'
                    , content: 'course_form.html'
                    , maxmin: true
                    , area: ['500px', '520px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        let iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-form-course-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            $.ajax({
                                url: setter.baseUrl + '/course/add',
                                data: JSON.stringify(data.field),
                                dataType: "json",
                                type: "post",
                                contentType: 'application/json',
                                xhrFields: {
                                    withCredentials: true
                                },
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg("成功");
                                        $(".layui-laypage-btn")[0].click();
                                        layer.close(index); //关闭弹层
                                    } else {
                                        layer.msg("失败");
                                    }

                                },
                                error: function (e) {
                                    layer.msg("失败");
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
